<?php
if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
if ($curruser['acl'] != "255")
{
    exit();
}
@ob_end_clean();
header("Pragma: no-cache");
header("Expires: 0");
header("Content-disposition: attachment; filename=" . G_DBDATABASE . "-" . date("Ymd") . ".sql");
header("Content-type: plain/text");
$tables = mysql_list_tables(G_DBDATABASE);
$num_tables = @mysql_numrows(@$tables);
$Year = date("Y");
$Month = date("m");
$Day = date("d");
$search = array("\x00", "\n", "\r", "\x1A");
$replace = array("\\0", "\\n", "\\r", "\\Z");
$DataCreation = strftime("%d %B %Y", mktime(0, 0, 0, $Month, $Day, $Year));
$Time = date("H:i:s");
$crlf = "\r\n";
$i = 0;
echo $crlf;
echo "# --------------------------------------------------------" . $crlf . "";
echo "#" . $crlf . "";
echo "# Dump Script for '" . G_DBDATABASE . "' " . $crlf . "";
echo "#" . $crlf . "";
echo "# Host: " . G_DBSERVER . "" . $crlf . "";
echo "#" . $crlf . "";
echo "# created on " . $DataCreation . " at " . $Time . "" . $crlf . "";
echo "#" . $crlf . "";
echo $crlf;
echo "# --------------------------------------------------------" . $crlf . "";
$schema_insert = "";
$table = "";
$schema_create = "";
while ($i < $num_tables)
{
    echo $crlf;
    $table = mysql_tablename($tables, $i);
    echo $crlf;
    echo "# --------------------------------------------------------" . $crlf . "";
    echo "#" . $crlf . "";
    echo "# Table Structure for '" . $table . "' " . $crlf . "";
    echo "#" . $crlf . "";
    echo $crlf;
    $schema_create .= "DROP TABLE IF EXISTS " . $table . ";" . $crlf . "";
    $schema_create .= "CREATE TABLE " . $table . " (" . $crlf . "";
    if (!( $result = mysql_db_query(G_DBDATABASE, "SHOW FIELDS FROM " . $table . "") ))
    {
        exit("error select database");
    }
    while ($row = mysql_fetch_array($result))
    {
        $schema_create .= "    {$row['Field']} {$row['Type']}";
        $schema_create .= isset($row['Default']) && (!empty($row['Default']) || $row['Default'] == "0" ) ? " DEFAULT '" . $row['Default'] . "'" : "";
        $schema_create .= $row['Null'] != "YES" ? " NOT NULL" : "";
        $schema_create .= $row['Extra'] != "" ? " " . $row['Extra'] . " " : "";
        $schema_create .= ",{$crlf}";
    }
    $schema_create = ereg_replace("," . $crlf . "\$", "", $schema_create);
    if (!( $result = mysql_db_query(G_DBDATABASE, "SHOW KEYS FROM " . $table . "") ))
    {
        exit("Can't show keys at {$table}");
    }
    $index = array();
    while ($row = mysql_fetch_array($result))
    {
        $kname = $row['Key_name'];
        if ($kname != "PRIMARY" && $row['Non_unique'] == 0)
        {
            $kname = "UNIQUE|" . $kname;
        }
        if (!isset($index[$kname]))
        {
            $index[$kname] = array();
        }
        $index[$kname][] = $row['Column_name'];
    }
    while (list( $x, $columns ) = x)
    {
        $schema_create .= ",{$crlf}";
        if ($x == "PRIMARY")
        {
            $schema_create .= "    PRIMARY KEY (" . implode($columns, ", ") . ")";
        }
        else if (substr($x, 0, 6) == "UNIQUE")
        {
            $schema_create .= "    UNIQUE " . substr($x, 7) . " (" . implode($columns, ", ") . ")";
        }
        else
        {
            $schema_create .= "    KEY " . $x . " (" . implode($columns, ", ") . ")";
        }
    }
    $schema_create .= "" . $crlf . ") ";
    if (!( $result = mysql_db_query(G_DBDATABASE, "SHOW TABLE STATUS FROM " . G_DBDATABASE . " LIKE '" . $table . "' ") ))
    {
        exit("Can't show table status at {$table}!");
    }
    $row = mysql_fetch_array($result);
    if (isset($row['Type']) && $row['Type'] != "")
    {
        $schema_create .= "Type=" . $row['Type'] . " ";
    }
    else
    {
        $schema_create .= "Type=MyISAM ";
    }
    $schema_create .= $row['Auto_increment'] == "NULL" || $row['Auto_increment'] == "" || !$row['Auto_increment'] ? "" : " AUTO_INCREMENT=" . $row['Auto_increment'] . " ";
    $schema_create .= ";{$crlf}{$crlf}";
    echo $schema_create;
    $schema_create = "";
    echo "#" . $crlf . "";
    echo "# Dumping data for table '" . $table . "'" . $crlf . "";
    echo "#" . $crlf . "";
    echo $crlf;
    if (!( $result = mysql_db_query(G_DBDATABASE, "SELECT * FROM {$table}") ))
    {
        exit("Can't select data from {$table}");
    }
    $a = 0;
    $table_list = "(";
    $field_list = "";
    $j = 0;
    while ($j < mysql_num_fields($result))
    {
        $field_list .= mysql_field_name($result, $j) . ", ";
        ++$j;
    }
    $field_list = substr($field_list, 0, 0 - 2);
    $field_list .= ")";
    $schema_insert = "";
    while ($row = mysql_fetch_row($result))
    {
        $schema_insert = "INSERT INTO " . $table . " " . ( isset($GLOBALS['showcolumns']) ? $field_list : "" ) . " VALUES (";
        $j = 0;
        while ($j < mysql_num_fields($result))
        {
            if (!isset($row[$j]))
            {
                $schema_insert .= " NULL,";
            }
            else if ($row[$j] != "")
            {
                $schema_insert .= " '" . str_replace($search, $replace, addslashes($row[$j])) . "',";
            }
            else
            {
                $schema_insert .= " '',";
            }
            ++$j;
        }
        $schema_insert = ereg_replace(",\$", "", $schema_insert);
        $schema_insert .= ");" . $crlf;
        echo $schema_insert;
        ++$a;
    }
    ++$i;
}
?>